package com.nondo.dean.product.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.product.domain.ProductBom;

import com.nondo.dean.product.mapper.ProductExtendsMapper;
import com.nondo.dean.product.model.inner.ProductBomBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.nondo.dean.product.domain.ProductBomExample;
import com.nondo.dean.product.mapper.ProductBomMapper;
import com.nondo.dean.product.model.ProductBomBean;

/**
 * @author xiaofan
 * @Description: 处理bom数据
 * @throws
 * @date 2018-04-0210:37
 */
@Component
public class ProductBomService {

    @Autowired
    ProductBomMapper productBomMapper;

    @Autowired
    ProductExtendsMapper productExtendsMapper;

    /**
     * 产品BOM结构入库
     * @param bomList
     * @return
     */
    public int synProductBom(List<ProductBomBeans> bomList){
        int count=0;
        try {
            //清除表数据
            delProductBom();
            int ma_index=1;
            for(ProductBomBeans bom : bomList){
                Set<String> productSet=new HashSet<String>();
                int dt_index=1;
                if(!"".equals(bom.getHeadDelete())){
                    continue;
                }
                String num=lpad((ma_index++)+"",4);
                bom.setNum(num);
                ProductBom productBom = new ProductBom();
                BeanUtils.copy(bom,productBom);
                productBomMapper.insertSelective(productBom);

                List<ProductBomBeans> childList=bom.getChild_List();
                for(ProductBomBeans childBom : childList){
                    if(!"".equals(childBom.getItemDelete())){ continue;}
                    if(!productSet.add(childBom.getProductCode())){continue;}
                    String dt_num=num+lpad((dt_index++)+"",2);
                    childBom.setNum(dt_num);
                    ProductBom childProductBom = new ProductBom();
                    BeanUtils.copy(childBom,childProductBom);
                    count = productBomMapper.insertSelective(childProductBom);
                }
            }
        } catch (Exception e) {
           e.printStackTrace();
           return 0;
        }
        return count;
    }

    /**
     * 清除产品BOM表所有数据
     * @param
     */

    public void delProductBom(){
       productExtendsMapper.delBom();
    }

    /**
     *返回编号，前面补0
     * @param str
     * @param i
     * @return
     */
    public  String lpad(String str, int i) {
        while (str.length() < i) {
            str = "0" + str;
        }
        return str;
    }

    /**
     * 根据父级编码查询商品BOM列表
     *
     * @param request
     * @param response
     * @param productCode 父级产品编码
     * @return 商品BOM列表信息 List<{@link ProductBom}>
     */
    public List<ProductBomBean> selectByParentProductCode(String productCode) {
        ProductBomExample example = new ProductBomExample();
        ProductBomExample.Criteria criteria = example.createCriteria();
        criteria.andParentProductCodeEqualTo(productCode);
        List<ProductBom> productBomList = productBomMapper.selectByExample(example);
        List<ProductBomBean> productBomBeanList = new ArrayList<ProductBomBean>();
        for (ProductBom productBom : productBomList) {
            ProductBomBean productBomBean = ProductBomBean.covertToBean(productBom);
            productBomBeanList.add(productBomBean);
        }
        return productBomBeanList;
    }
}
